<?php
/**
 * 企业
 * Created by PhpStorm.
 * User: xsh
 * Date: 2018/4/16  17:00
 */

namespace app\pc\Controller;


class Project extends Base
{
    /*
    *	构造函数
    */
    function __construct(){
        parent::__construct();
    }

    //首页
    public function index(){
        $where = '';
        $whereor_item_name = '';
        $whereor_certificate_no = '';
        $project_result = [];
        $uid = $this->info_user['id'];

        $company_name = input('company_name');
        $project_name = input('project_name');
        $certificate_no = input('certificate_no');
        $order = input('order');

        if($order == ''){
            $order = 'xp.time_batch desc';
        }else{
            if($order == 'desc'){
                $order = 'xp.time_batch desc';
            }else{
                $order = 'xp.time_batch asc';
            }
        }

        if($company_name != '')
        {
            $where['xc.name'] = array('like','%'.$company_name.'%');
        }
        if($project_name != '')
        {
            $whereor_item_name['xp.item_name'] = array('like','%'.$project_name.'%');
        }
        if($certificate_no != '')
        {
            $whereor_certificate_no['xp.certificate_no'] = array('like','%'.$certificate_no.'%');
        }
        $province = input('province');
        if($province != "")
        {
            $where['province'] = $province;
        }

        $city = input('city');
        if($city != "")
        {
            $where['city'] = $city;
        }

        $area = input('area');
        if($area != "")
        {
            $where['area'] = $area;
        }

        $result = member_right_num($uid,'project');

        //查询条数
        $project_count = db('xls_project')->alias('xp')
            ->join('xls_company xc','xc.id = xp.company_id')
            ->field('xp.*,xc.name,xc.city')
            ->count();

        if(isset($result['bool'])){
            if($result['bool']){
                if($result['is_infinite'] == '-1'){
                    $limit = $result['num'];
                }else{
                    $limit = $project_count;
                } 
                $project_result = db('xls_project')->alias('xp')
                    ->where($where)
                    ->whereor($whereor_item_name)
                    ->whereor($whereor_certificate_no)
                    ->join('xls_company xc','xc.id = xp.company_id')
                    ->field('xp.*,xc.name,xc.city,xc.id as company_id')
                    ->order($order)
                    ->paginate($limit,$limit);
            }
        }
        //halt($project_result);
        $this->assign('project_result',$project_result);
        $this->assign('project_count',$project_count);
        $this->assign('page',$project_result->render());
        return $this->fetch('project/list');
    }

    //关注
    public function follow(){
        $attention = input('attention');
        $company_id = input('company_id');
        $status = input('status');
        $uid = $this->info_user['id'];

        if(request()->isAjax()) {
            if($status == '1'){
                $result = member_right_num($uid,'enterprise_follow_num');
                if(isset($result['bool'])){
                    if(!$result['bool']){
                        $this->error('你没有关注权利或者你关注次数已用完');
                    }
                }
            }


            $result_attention = db('attention')->where(array('company_id'=>$company_id,'uid'=>$uid))->find();

            if(empty($result_attention)){
                $data['uid'] = $uid;
                $data['company_id'] = $company_id;
                $data['status'] = 1;
                db('attention')->insert($data);
            }else{
                if($result_attention['status'] == '1'){
                    $this->error('已关注过了');
                }

                db('attention')->where(array('uid'=>$uid,'company_id'=>$company_id))->update(array('status'=>'1'));
            }
        }

    }

    //企业详情
    public function company_info(){
        $company_id = input('company_id');

        $project = db('xls_project')->where(array('company_id'=>$company_id))->select();

        $attention_result = db('attention')->where(array('uid'=>$uid,'company_id'=>$company_id))->field('status')->find();

        $this->assign('attention_result',$attention_result);
        $this->assign('project',$project);
        return $this->fetch();
    } 
}